<?php

namespace App\Admin\Renderable;

use App\Admin\Repositories\CourseSource;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class CourseSourceTable extends LazyRenderable
{
    public function grid(): Grid
    {
        return Grid::make(new CourseSource(['class']), function (Grid $grid){
            $grid->model()->orderByDesc('id');

            $grid->column('id', 'ID');
            $grid->column('name');
            $grid->column('class.name', '分类名称');
            $grid->column('cover')->image(' ', 50);
            $grid->column('file', '视频')->display('预览')->modal(function ($modal){
                $modal->title('预览');
                $modal->icon('');

                $file = $this->file;

                return <<<HTML
<div><video src="{$file}" controls="controls" autoplay width="100%"></video></div>
HTML;

            });
            $grid->column('sort');
            $grid->column('created_at');

            $grid->paginate(10);
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->equal('course_source_class_id', '分类')->select(\App\Models\CourseSourceClass::orderByDesc('sort')->pluck('name', 'id')->all())->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }
}
